/*
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-03-15 11:36:28
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-03-16 09:22:11
 */
'use static'
const {logger} = require('./logger')
const success = async (ctx,next)=>{
    console.log('返回结果：',ctx.result)
    if(ctx.result!==undefined){
         ctx.type = 'json'
         ctx.body = {
             code: 200,
             msg: ctx.msg || '成功',
             data: ctx.result
         }
    }
    // console.log(ctx.body)
    await next();
}
const error = async (ctx,next)=>{
    // console.log('错误:', ctx)
    await next().catch(err=>{
        if(err.code==null){
            logger.error(err.stack)
        }
        ctx.body={
            code:err.code||-1,
            data:null,
            msg:err.message.trim()
        }
        //ctx.status=200;
        return Promise.reject();
    })
}


module.exports={
    success,
    error
}